﻿/// <reference path="~/Scripts/Library/jquery-1.9.1.min.js" />

var postbackController = "EnterpriseManagement";

function ConfigurationItem_Validate() {

    var isValid = true;

    var lastResponseMessageLabel = $("#LastResponseMessageLabel");


    if ($("#Environment_DatabaseConfiguration_PoolingEnabled")[0].checked) {

        var poolingSizeMinimum = parseInt($("#Environment_DatabaseConfiguration_PoolSizeMinimum").val());

        var poolingSizeMaximum = parseInt($("#Environment_DatabaseConfiguration_PoolSizeMaximum").val());


        if (((isNaN(poolingSizeMaximum)) || (isNaN(poolingSizeMinimum)))

            || (poolingSizeMinimum > poolingSizeMaximum) 

            || (poolingSizeMinimum < 0))  {

            LastResponseMessage_SetError("Valid sizes a required when Connection Pooling is enabled.");

            $("#Environment_DatabaseConfiguration_PoolSizeMinimum").closest("tr").children().first().addClass("ui-state-error");

            $("#Environment_DatabaseConfiguration_PoolSizeMaximum").closest("tr").children().first().addClass("ui-state-error");

            isValid = false;

        }

    }

    if (($("#Environment_DatabaseConfiguration_TrustedConnection")[0].checked == false) && ($("#Environment_DatabaseConfiguration_UserName").val () == "")) { 
        
        lastResponseMessageLabel.html("User Name is required when not using a Trusted Connection.");

        lastResponseMessageLabel.parent().addClass("ui-state-error");

        $("#Environment_DatabaseConfiguration_UserName").closest("tr").children().first().addClass("ui-state-error");

        isValid = false;

    }


    if ($("#Environment_DatabaseConfiguration_DatabaseName").val() == "") {

        lastResponseMessageLabel.html("The Database Name must not be empty.");

        lastResponseMessageLabel.parent().addClass("ui-state-error");

        $("#Environment_DatabaseConfiguration_DatabaseName").closest("tr").children().first().addClass("ui-state-error");

        isValid = false;

    }

    if ($("#Environment_DatabaseConfiguration_ServerName").val() == "") {

        lastResponseMessageLabel.html("The Server Name must not be empty.");

        lastResponseMessageLabel.parent().addClass("ui-state-error");

        $("#Environment_DatabaseConfiguration_ServerName").closest("tr").children().first().addClass("ui-state-error");

        isValid = false;

    }

    if (!isValid) {

        $($("#PropertyTabs>ul>li")[1]).addClass("ui-state-error");

    }

    return isValid;

}

